# -*- coding: utf-8 -*-
# @Time    : 2023/10/31 14:39
# @Author  : floraachy
# @File    : test_repo_issue_flow.py
# @Software: PyCharm
# @Desc:

# 标准库导入
import os
# 第三方库导入
from playwright.sync_api import expect
# 本地应用/模块导入
from case_utils.base_page import BasePage
from case_utils.allure_handle import allure_step


class RepoIssuePage(BasePage):
    def click_create_issue_button(self):
        """
        点击"创建疑修"按钮
        """
        self.click(locator="//a[text()='创建疑修']")
        self.page.wait_for_load_state()

    def input_issue_title(self, issue_title: str):
        """
        输入疑修标题
        """
        self.input(locator="id=subject", text=issue_title)

    def input_issue_desc(self, issue_desc: str):
        """
        输入疑修内容
        """
        # todo 疑惑：这里必须加wait_for_timeout，否则即使用的wait_for_selector，也无法输入内容。
        self.page.wait_for_timeout(1000)
        # elem = page.wait_for_selector(
        #     selector="//textarea/following::div[contains(@class, 'CodeMirror-wrap')]", timeout=10000, state='visible')
        elem = self.page.query_selector(selector="//textarea/following::div[contains(@class, 'CodeMirror-wrap')]")
        self.execute_js('''([elem, content]) => elem.CodeMirror.setValue(content);''',
                        [elem, issue_desc])

    def submit_issue(self):
        """
        点击"创建"按钮，提交表单校验，进行疑修创建
        """
        self.click(locator="//span[text()='创 建']/parent::button")

    def check_issue_success_text(self, text):
        """
        疑修创建成功，有成功提示"任务创建成功！"
        """
        self.contain_text(locator="xpath=//div[@class='ant-notification-notice-description']", text=text)

    def check_issue_title(self, title: str):
        """
        检查易修标题
        """
        self.contain_text(locator="//div[@class='detailtitle']/div/p", text=title)

    def check_issue_desc(self, desc: str):
        """
        检查易修内容
        """
        self.contain_text(locator="//div[@class='descPanel']/div/p", text=desc)

    def check_issue_attachments(self, files):
        """
        检查易修附件
        """
        actual_issue_files = self.get_all_elements_text(
            locator="//div[contains(@class, 'attachment-list-div')]//span[1]")
        expected_issue_files = [os.path.basename(file_path) for file_path in files]
        allure_step(step_title=f"预期结果：{expected_issue_files} = 实际结果：{actual_issue_files}")
        assert expected_issue_files == actual_issue_files
